//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Query Application Block
//===============================================================================

using System;
using System.Data;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design.Validation;
using EntLibContrib.Query.Properties;

namespace EntLibContrib.Query.Configuration.Design
{
  /// <summary>
	/// Represents a <see cref="DataParameterData"/> configuration element.
  /// </summary>
  public sealed class DataParameterNode : ParameterNode
	{
		#region Fields
		private DbType dbType;
		private ParameterDirection direction;
		#endregion

		#region Properties
		/// <summary>
		/// Gets or sets the DbType for the current parameter.
		/// </summary>
		/// <value>
		/// The DbType for the current parameter
		/// </value>
		[SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "Db"), Required]
		[QueryCategory("CategoryGeneral")]
		[QueryDescription("DataParameterDbTypeDescription")]
		public DbType DbType
		{
			get { return dbType; }
			set { dbType = value; }
		}

		/// <summary>
		/// Gets or sets the Direction for the current parameter.
		/// </summary>
		/// <remarks>The Direction is relative to the data source</remarks>
		/// <value>
		/// The Direction for the current parameter
		/// </value>
		[Required]
		[QueryCategory("CategoryGeneral")]
		[QueryDescription("DataParameterDirectionDescription")]
		public ParameterDirection Direction
		{
			get { return direction; }
			set { direction = value; }
		}

		/// <summary>
		/// Gets the <see cref="ParameterData"/> this node represents.
		/// </summary>
		/// <value>
		/// The <see cref="ParameterData"/> this node represents.
		/// </value>
		[ReadOnly(true)]
		public override ParameterData ParameterData
		{
			get { return new DataParameterData(Name, dbType, direction); }
		}
		#endregion

		#region Construction
		/// <summary>
		/// Initialize a new instance of the <see cref="DataParameterNode"/> class.
		/// </summary>
		public DataParameterNode()
			: this(new DataParameterData(InternalResources.DefaultDataParameterNodeName, DbType.Guid, ParameterDirection.Input))
    {
		}

		/// <summary>
		/// Initialize a new instance of the <see cref="DataParameterNode"/> class with an <see cref="DataParameterData"/> instance.
		/// </summary>
		/// <param name="dataParameterData">The data parameter data.</param>
		public DataParameterNode(DataParameterData dataParameterData)
    {
			if (dataParameterData == null)
      {
				throw new ArgumentNullException("dataParameterData");
      }

			Rename(dataParameterData.Name);
			this.dbType = dataParameterData.DbType;
			this.direction = dataParameterData.Direction;
		}
		#endregion

		#region Public Methods
		/// <summary>
		/// Clones this instance.
		/// </summary>
		/// <returns></returns>
		public override ParameterNode Clone()
		{
			return new DataParameterNode((DataParameterData)this.ParameterData);
		}
		#endregion
	}
}